package com.study.d8_net_udp_mul;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
////多发多收
public class Server {
    public static void main(String[] args) throws Exception{
        //目标：开发一个服务端程序。（接受端）
        System.out.println("服务器启动成功！");
        //1、创建接收端对象，注册端口
        DatagramSocket socket = new DatagramSocket(8888);

        //2、创建一个数据包对象
        byte[] bytes = new byte[1024*64];
        DatagramPacket packet = new DatagramPacket(bytes,bytes.length);

        while (true) {
            //3、接收数据
            socket.receive(packet);

            //4、把数据输出
            int len = packet.getLength();
            String str = new String(bytes,0,len);

            System.out.println(str);
            //获取发送方端口和ip
            InetAddress address = packet.getAddress();
            System.out.println("发送端ip："+address.getHostAddress());
            System.out.println("发送端端口号："+packet.getPort());
            System.out.println("====================================");
            //不用关闭
        }
    }
}
